
var express = require('express');
var router = express.Router();
var api = require('../ai');
var fs = require('fs');

var multer = require('multer');

var uploadImage = multer({
    storage: multer.diskStorage({
        destination: 'public/uploads/',
    }),
    fileFilter: function (req, file, callback) {
        if (!file.mimetype.match(/^image\//i)) {
            return callback('只能上传图片文件!', false);
        }
        callback(null, true);
    },
    limits: {
        fileSize: 30 * 1024 * 1024
    }
});

router.post('/imageCensorUserDefined', uploadImage.any(), function (req, res, next) {
    if (req.files && req.files.length) {
        api.contentCensor.imageCensorUserDefined(new Buffer(fs.readFileSync(`${process.cwd()}/${req.files[0].path}`)).toString('base64'), 'base64').then(function (result) {
            console.log(JSON.stringify(result));
            res.json({
                code: 0,
                msg: 'suc',
                data: result
            });
        }).catch(function (err) {
            // 如果发生网络错误
            console.log(err);
            res.json({
                code: 1,
                msg: '上传出错!'
            })
        });
    } else {
        res.json({
            code: 1,
            msg: '上传出错!'
        })
    }
});

module.exports = router;
